<template>
  <div class="user-detail-panel">
    <!-- 用户基本信息 -->
    <div class="user-info-section">
      <h3>用户基本信息</h3>
      <el-row :gutter="24">
        <el-col :span="12">
          <div class="info-item">
            <span class="label">用户ID:</span>
            <span class="value">{{ user.userId }}</span>
          </div>
          <div class="info-item">
            <span class="label">用户姓名:</span>
            <span class="value">{{ user.userName }}</span>
          </div>
          <div class="info-item">
            <span class="label">注册时间:</span>
            <span class="value">{{ user.registerDate || '2023-06-15' }}</span>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="info-item">
            <span class="label">最后活跃:</span>
            <span class="value">{{ user.lastActiveDate }}</span>
          </div>
          <div class="info-item">
            <span class="label">用户等级:</span>
            <span class="value">{{ user.userLevel || 'VIP' }}</span>
          </div>
          <div class="info-item">
            <span class="label">联系方式:</span>
            <span class="value">{{ user.phone || '138****5678' }}</span>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 购买历史统计 -->
    <div class="purchase-stats-section">
      <h3>购买历史统计</h3>
      <el-row :gutter="24">
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-value">{{ user.totalOrders }}</div>
            <div class="stat-label">总订单数</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-value">¥{{ user.totalSpent.toLocaleString() }}</div>
            <div class="stat-label">总消费金额</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-value">¥{{ user.avgOrderValue.toLocaleString() }}</div>
            <div class="stat-label">平均订单金额</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-card">
            <div class="stat-value">{{ calculateReturnRate() }}%</div>
            <div class="stat-label">复购率</div>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 预测因子分析 -->
    <div class="prediction-factors-section" v-if="predictionData">
      <h3>预测因子分析</h3>
      <div class="factors-list">
        <div 
          v-for="factor in predictionData.factors" 
          :key="factor.name"
          class="factor-item"
          :class="factor.impact"
        >
          <div class="factor-info">
            <span class="factor-name">{{ factor.name }}</span>
            <span class="factor-impact">{{ getImpactText(factor.impact) }}</span>
          </div>
          <div class="factor-value">
            <el-progress 
              :percentage="factor.value" 
              :stroke-width="8"
              :color="getImpactColor(factor.impact)"
              :show-text="false"
            />
            <span class="value-text">{{ factor.value }}%</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 行为轨迹 -->
    <div class="behavior-timeline-section">
      <h3>最近行为轨迹</h3>
      <el-timeline>
        <el-timeline-item
          v-for="activity in recentActivities"
          :key="activity.id"
          :timestamp="activity.timestamp"
          :color="getActivityColor(activity.type)"
        >
          <div class="activity-content">
            <div class="activity-title">{{ activity.title }}</div>
            <div class="activity-description">{{ activity.description }}</div>
          </div>
        </el-timeline-item>
      </el-timeline>
    </div>

    <!-- 推荐建议 -->
    <div class="recommendations-section" v-if="predictionData">
      <h3>推荐建议</h3>
      <div class="recommendations-list">
        <div 
          v-for="(recommendation, index) in predictionData.recommendations" 
          :key="index"
          class="recommendation-item"
        >
          <el-icon class="recommendation-icon"><Star /></el-icon>
          <span class="recommendation-text">{{ recommendation }}</span>
        </div>
      </div>
    </div>

    <!-- 操作按钮 -->
    <div class="action-buttons">
      <el-button type="primary" @click="createStrategy">制定挽回策略</el-button>
      <el-button @click="viewFullProfile">查看完整画像</el-button>
      <el-button @click="exportUserData">导出用户数据</el-button>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { ElMessage } from 'element-plus'
import { Star } from '@element-plus/icons-vue'

// Props
const props = defineProps({
  user: {
    type: Object,
    required: true
  },
  predictionData: {
    type: Object,
    default: null
  }
})

// Emits
const emit = defineEmits(['create-strategy', 'view-profile', 'export-data'])

// 模拟最近活动数据
const recentActivities = computed(() => [
  {
    id: 1,
    type: 'login',
    title: '用户登录',
    description: '通过移动端APP登录',
    timestamp: '2024-01-15 14:30'
  },
  {
    id: 2,
    type: 'browse',
    title: '浏览商品',
    description: '浏览了电子产品分类下的5个商品',
    timestamp: '2024-01-15 14:25'
  },
  {
    id: 3,
    type: 'cart',
    title: '加入购物车',
    description: '将iPhone 15 Pro加入购物车',
    timestamp: '2024-01-15 14:20'
  },
  {
    id: 4,
    type: 'purchase',
    title: '完成购买',
    description: '购买了智能手表，订单金额¥2299',
    timestamp: '2024-01-10 16:45'
  },
  {
    id: 5,
    type: 'service',
    title: '客服咨询',
    description: '咨询了产品保修相关问题',
    timestamp: '2024-01-08 10:15'
  }
])

// 方法
const calculateReturnRate = () => {
  // 模拟计算复购率
  return Math.round((props.user.totalOrders / (props.user.totalOrders + 5)) * 100)
}

const getImpactText = (impact) => {
  const textMap = {
    'high': '高影响',
    'medium': '中影响',
    'low': '低影响'
  }
  return textMap[impact] || '未知'
}

const getImpactColor = (impact) => {
  const colorMap = {
    'high': '#F56C6C',
    'medium': '#E6A23C',
    'low': '#67C23A'
  }
  return colorMap[impact] || '#909399'
}

const getActivityColor = (type) => {
  const colorMap = {
    'login': '#409EFF',
    'browse': '#67C23A',
    'cart': '#E6A23C',
    'purchase': '#F56C6C',
    'service': '#909399'
  }
  return colorMap[type] || '#909399'
}

const createStrategy = () => {
  emit('create-strategy', props.user)
}

const viewFullProfile = () => {
  emit('view-profile', props.user)
  ElMessage.info('跳转到用户完整画像页面')
}

const exportUserData = () => {
  emit('export-data', props.user)
  
  // 模拟导出用户数据
  const userData = {
    basicInfo: props.user,
    predictionData: props.predictionData,
    activities: recentActivities.value
  }
  
  const dataStr = JSON.stringify(userData, null, 2)
  const dataBlob = new Blob([dataStr], { type: 'application/json' })
  const url = URL.createObjectURL(dataBlob)
  
  const link = document.createElement('a')
  link.href = url
  link.download = `user_${props.user.userId}_detail.json`
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
  URL.revokeObjectURL(url)
  
  ElMessage.success('用户数据已导出')
}
</script>

<style scoped>
.user-detail-panel {
  padding: 20px 0;
}

.user-info-section,
.purchase-stats-section,
.prediction-factors-section,
.behavior-timeline-section,
.recommendations-section {
  margin-bottom: 32px;
}

.user-info-section h3,
.purchase-stats-section h3,
.prediction-factors-section h3,
.behavior-timeline-section h3,
.recommendations-section h3 {
  margin: 0 0 16px 0;
  color: #333;
  font-size: 16px;
  font-weight: 600;
  border-bottom: 2px solid #f0f0f0;
  padding-bottom: 8px;
}

/* 用户信息 */
.info-item {
  display: flex;
  margin-bottom: 12px;
}

.info-item .label {
  width: 80px;
  color: #666;
  font-size: 14px;
}

.info-item .value {
  color: #333;
  font-size: 14px;
  font-weight: 500;
}

/* 统计卡片 */
.stat-card {
  text-align: center;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
}

.stat-value {
  font-size: 20px;
  font-weight: 700;
  color: #333;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 12px;
  color: #666;
}

/* 预测因子 */
.factors-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.factor-item {
  padding: 16px;
  border-radius: 8px;
  border-left: 4px solid #ddd;
}

.factor-item.high {
  background: #fef2f2;
  border-left-color: #F56C6C;
}

.factor-item.medium {
  background: #fffbeb;
  border-left-color: #E6A23C;
}

.factor-item.low {
  background: #f0f9ff;
  border-left-color: #67C23A;
}

.factor-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.factor-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.factor-impact {
  font-size: 12px;
  color: #666;
}

.factor-value {
  display: flex;
  align-items: center;
  gap: 12px;
}

.factor-value .el-progress {
  flex: 1;
}

.value-text {
  font-size: 14px;
  font-weight: 600;
  color: #333;
  min-width: 40px;
}

/* 活动时间线 */
.activity-content {
  padding-left: 8px;
}

.activity-title {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin-bottom: 4px;
}

.activity-description {
  font-size: 13px;
  color: #666;
}

/* 推荐建议 */
.recommendations-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.recommendation-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  background: #f0f9ff;
  border-radius: 8px;
}

.recommendation-icon {
  color: #409EFF;
  font-size: 16px;
}

.recommendation-text {
  font-size: 14px;
  color: #333;
}

/* 操作按钮 */
.action-buttons {
  display: flex;
  gap: 12px;
  justify-content: center;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

@media (max-width: 768px) {
  .action-buttons {
    flex-direction: column;
  }
  
  .action-buttons .el-button {
    width: 100%;
  }
}
</style>
